package com.turing.post.common.impl;

import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.MerchantOper;
import com.turing.post.common.util.MerchantOperDao;
import com.turing.post.util.Config;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;

/**
 * 商户操作员数据访问实现类
 * 
 * @author 李治
 * 
 */
@Stateless(mappedName = "common.impl.MerchantOperDaoImpl")
@Remote
@Local
public class MerchantOperDaoImpl implements MerchantOperDao {
	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public void deleteMerchantOper(BigInteger merchantOperId)
			throws PCPSApplicationException {
		MerchantOper oper = getMerchantOperByMerchantOperId(merchantOperId);
		if (null != oper)
			GeneralDao.remove(_em, oper);
	}

	@Override
	public MerchantOper getMerchantOperByMerchantOperId(
			BigInteger merchantOperId) {
		return _em.find(MerchantOper.class, merchantOperId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public MerchantOper getMerchantOperByMrchOperLogonName(
			String mrchOperLogonName) {
		String sql = "select * from merchantoper where mrchoperlogonname = ?1";
		Query query = _em.createNativeQuery(sql, MerchantOper.class)
				.setParameter(1, mrchOperLogonName);
		List<MerchantOper> list = query.getResultList();
		if (list == null || list.isEmpty() || list.size() < 1) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public MerchantOper getMerchantOperQuestionByMrchOperLogonName(
			String mrchOperLogonName) {
		MerchantOper merchantOper = getMerchantOperByMrchOperLogonName(mrchOperLogonName);
		MerchantOper returnMerchantOper = new MerchantOper();
		returnMerchantOper.setQuestion1(merchantOper.getQuestion1());
		returnMerchantOper.setQuestion2(merchantOper.getQuestion2());
		returnMerchantOper.setQuestion3(merchantOper.getQuestion3());
		return returnMerchantOper;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantOper> getMerchantOpers(Integer page) {
		String sql = "select * from merchantoper";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MerchantOper.class)
				.setFirstResult(startPosition).setMaxResults(maxResult);
		return query.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MerchantOper> getMerchantOpersByMerchantNo(String merchantNo,
			Integer page) {
		String sql = "select * from merchantoper where merchantno = ?1";
		int maxResult = Config.PAGE_SIZE;
		int startPosition = maxResult * page;
		Query query = _em.createNativeQuery(sql, MerchantOper.class)
				.setParameter(1, merchantNo).setFirstResult(startPosition)
				.setMaxResults(maxResult);
		return query.getResultList();
	}

	@Override
	public Integer getMerchantOpersCount() {
		String sql = "select nvl(count(*),0) from merchantoper";
		Query query = _em.createNativeQuery(sql);
		return Integer.valueOf(query.getSingleResult().toString());
	}

	@Override
	public Integer getMerchantOpersCountByMerchantNo(String merchantNo) {
		String sql = "select nvl(count(*),0) from merchantoper where merchantno = ?1";
		Query query = _em.createNativeQuery(sql).setParameter(1, merchantNo);
		return Integer.valueOf(query.getSingleResult().toString());
	}

	@Override
	public void insertMerchantOper(MerchantOper merchantOper)
			throws PCPSApplicationException {
		merchantOper.setMrchOperId(getMaxOperId());
		GeneralDao.insert(_em, merchantOper);
	}

	@Override
	public void updateMerchantOper(MerchantOper merchantOper)
			throws PCPSApplicationException {
		GeneralDao.update(_em, merchantOper);
	}

	@Override
	public BigInteger getMaxOperId() {
		Query query = _em
				.createNativeQuery("select nvl(max(mrchOperId),0)+1 mrchOperId from merchantoper");
		return new BigInteger(query.getSingleResult().toString());
	}

	
}
